/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.widget.item.impl;

import com.genia.toolbox.web.gwt.basics.client.GwtHelper;
import com.genia.toolbox.web.gwt.basics.client.widget.FileUploadWidget;
import com.genia.toolbox.web.gwt.form.client.CssNames;
import com.genia.toolbox.web.gwt.form.client.form.FileUploadItem;
import com.genia.toolbox.web.gwt.form.client.widget.item.ItemWidgetModifyListener;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * widget that represents a file upload control.
 */
public class FileUploadItemWidget
    extends AbstractSimpleValuedItemWidget<FileUploadItem>
{

  /**
   * the {@link FileUploadWidget} to use.
   */
  private final transient FileUploadWidget fileUploadWidget = new FileUploadWidget();

  /**
   * the panel presenting the file.
   */
  private final transient HorizontalPanel panel = new HorizontalPanel();

  /**
   * the list of all the {@link Widget} that must be modify to update the error
   * message.
   */

  private final transient Widget[] widgetsToToggle = new Widget[] { panel };



  /**
   * constructor.
   * 
   * @param item
   *          the item this object represents
   */
  public FileUploadItemWidget(FileUploadItem item)
  {
    super(item);

    final Label uploadLabelWidget = new Label();
    GwtHelper.fillText(uploadLabelWidget, item.getLabelKey());

    panel.add(uploadLabelWidget);
    panel.add(fileUploadWidget);

    // set the styleName.
    panel.addStyleName(CssNames.FILE_UPLOAD_PANEL_CSS_STYLE);
    panel.addStyleName(item.getName());

    initWidget(panel);
  }



  /**
   * add a listener that is called each time the value of this object change.
   * 
   * @param modifyListener
   *          the callback
   * @see com.genia.toolbox.web.gwt.form.client.widget.item.SimpleItemWidget#addModifyListener(com.genia.toolbox.web.gwt.form.client.widget.item.ItemWidgetModifyListener)
   */
  public void addModifyListener(final ItemWidgetModifyListener modifyListener)
  {
    fileUploadWidget.addChangeListener(new ChangeListener()
    {
      public void onChange(final Widget widget)
      {
        modifyListener.onModify(FileUploadItemWidget.this);
      }
    });
  }



  /**
   * returns the current value of this item.
   * 
   * @return a <code>String</code> representing the current value of this item
   * @see com.genia.toolbox.web.gwt.form.client.widget.item.impl.AbstractSimpleValuedItemWidget#getValue()
   */
  @Override
  public String getValue()
  {
    return fileUploadWidget.getValue();
  }



  /**
   * set the current value of this item.
   * 
   * @param value
   *          a <code>String</code> representing the new value of this item
   * @see com.genia.toolbox.web.gwt.form.client.widget.item.impl.AbstractSimpleValuedItemWidget#setValue(java.lang.String)
   */
  @Override
  public void setValue(String value)
  {
    fileUploadWidget.setValue(value);
  }



  /**
   * returns an array containing all the widget that the setErrorMessage must
   * modify.
   * 
   * @return an array containing all the widget that the setErrorMessage must
   *         modify
   * @see com.genia.toolbox.web.gwt.form.client.widget.item.impl.AbstractSimpleItemWidget#getWidgetsToToggle()
   */
  @Override
  protected Widget[] getWidgetsToToggle()
  {
    return widgetsToToggle;
  }

}
